set(TARGET_NAME GnssLogs)

add_compile_definitions(GNSSLOGS_LIB)

find_package(pegtl CONFIG REQUIRED)
find_package(Boost REQUIRED)

file(GLOB HEADERS "*.h")
file(GLOB SOURCES "*.cpp")
file(GLOB DECODERS "Decoders/*.h" "Decoders/*.cpp")

if(MSVC)
    set(GNSSLOGS_VERSIONINFO_RC ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.rc)
    configure_file("${PROJECT_SOURCE_DIR}/cmake/${TARGET_NAME}.rc.in" "${GNSSLOGS_VERSIONINFO_RC}")
    list(APPEND RESOURCES ${GNSSLOGS_VERSIONINFO_RC}) 
endif(MSVC)

set(GNSSLOGS_FILES ${HEADERS})
foreach(_A ${GNSSLOGS_FILES})
    get_filename_component(_FILE ${_A} NAME_WE)
    if(NOT (_FILE STREQUAL "Commons"
        OR _FILE STREQUAL "Flags"
        OR _FILE STREQUAL "FormatMacro"
        OR _FILE STREQUAL "GnssLogs"
        OR _FILE STREQUAL "gnsslogs_global"
        OR _FILE STREQUAL "LogHeader"
        OR _FILE STREQUAL "GnssLogVariant"))
            string(APPEND GNSSLOGS_DATAS_H "#include \"${_FILE}.h\"\n")
            string(APPEND _GNSSLOGS_DATAS "${_FILE}::Data, ")
    endif()
endforeach()
string(LENGTH ${_GNSSLOGS_DATAS} _STRLEN)
math(EXPR _STRLEN "${_STRLEN} - 2")
string(SUBSTRING ${_GNSSLOGS_DATAS} 0 ${_STRLEN} GNSSLOGS_DATAS)
set(GNSSLOGVARIANT_H ${CMAKE_CURRENT_LIST_DIR}/GnssLogVariant.h)
configure_file("${PROJECT_SOURCE_DIR}/cmake/GnssLogVariant.h.in" "${GNSSLOGVARIANT_H}")
list(APPEND HEADERS ${GNSSLOGVARIANT_H}) 

set(GNSSLOGS_VERSION_H ${CMAKE_CURRENT_BINARY_DIR}/Version.h)
configure_file("${PROJECT_SOURCE_DIR}/cmake/Version.h.in" "${GNSSLOGS_VERSION_H}")
list(APPEND HEADERS ${GNSSLOGS_VERSION_H}) 


source_group("Headers" FILES ${HEADERS})
source_group("Sources" FILES ${SOURCES})
source_group("Decoders" FILES ${DECODERS})

add_library(${TARGET_NAME} SHARED ${HEADERS} ${SOURCES} ${DECODERS} ${RESOURCES})
set_target_properties(${TARGET_NAME} PROPERTIES PUBLIC_HEADER "${HEADERS}")

target_compile_options(${TARGET_NAME} 
    PRIVATE 
    $<$<CXX_COMPILER_ID:MSVC>:/bigobj> 
    $<$<CXX_COMPILER_ID:GNU>:-Wa,-mbig-obj>) 

target_link_libraries(${TARGET_NAME} PRIVATE taocpp::pegtl Boost::boost)
export_install_target(${TARGET_NAME} ${INSTALL_INCLUDEDIR} ${INSTALL_LIBRARYDIR} ${INSTALL_BINDIR})
